﻿namespace UnityEngine
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;

    public sealed class WWW : IDisposable
    {
        private IntPtr m_Ptr;

        public WWW(string url)
        {
            this.InitWWW(url, null, null);
        }

        public WWW(string url, WWWForm form)
        {
            this.InitWWW(url, form.data, FlattenedHeadersFrom(form.headers));
        }

        public WWW(string url, byte[] postData)
        {
            this.InitWWW(url, postData, null);
        }

        public WWW(string url, byte[] postData, Hashtable headers)
        {
            this.InitWWW(url, postData, FlattenedHeadersFrom(headers));
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        internal extern WWW(string url, int version, uint crc);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern void DestroyWWW(bool cancel);
        public void Dispose()
        {
            this.DestroyWWW(true);
        }

        public static string EscapeURL(string s)
        {
            Encoding e = Encoding.UTF8;
            return EscapeURL(s, e);
        }

        public static string EscapeURL(string s, Encoding e)
        {
            if (s == null)
            {
                return null;
            }
            if (s == string.Empty)
            {
                return string.Empty;
            }
            if (e == null)
            {
                return null;
            }
            return WWWTranscoder.URLEncode(s, e);
        }

        ~WWW()
        {
            this.DestroyWWW(false);
        }

        private static string[] FlattenedHeadersFrom(Hashtable headers)
        {
            if (headers == null)
            {
                return null;
            }
            string[] strArray = new string[headers.Count * 2];
            int num = 0;
            IDictionaryEnumerator enumerator = headers.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    DictionaryEntry current = (DictionaryEntry) enumerator.Current;
                    strArray[num++] = current.Key.ToString();
                    strArray[num++] = current.Value.ToString();
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable == null)
                {
                }
                disposable.Dispose();
            }
            return strArray;
        }

        public AudioClip GetAudioClip(bool threeD)
        {
            return this.GetAudioClip(threeD, false);
        }

        public AudioClip GetAudioClip(bool threeD, bool stream)
        {
            return this.GetAudioClip(threeD, stream, AudioType.UNKNOWN);
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern AudioClip GetAudioClip(bool threeD, bool stream, AudioType audioType);
        private Encoding GetTextEncoder()
        {
            string str = null;
            if (this.responseHeaders.TryGetValue("CONTENT-TYPE", out str))
            {
                int index = str.IndexOf("charset", StringComparison.OrdinalIgnoreCase);
                if (index > -1)
                {
                    int num2 = str.IndexOf('=', index);
                    if (num2 > -1)
                    {
                        char[] trimChars = new char[] { '\'', '"' };
                        string name = str.Substring(num2 + 1).Trim().Trim(trimChars).Trim();
                        int length = name.IndexOf(';');
                        if (length > -1)
                        {
                            name = name.Substring(0, length);
                        }
                        try
                        {
                            return Encoding.GetEncoding(name);
                        }
                        catch (Exception)
                        {
                            Debug.Log("Unsupported encoding: '" + name + "'");
                        }
                    }
                }
            }
            return Encoding.UTF8;
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private extern Texture2D GetTexture(bool markNonReadable);
        [Obsolete("All blocking WWW functions have been deprecated, please use one of the asynchronous functions instead.", true)]
        public static Texture2D GetTextureFromURL(string url)
        {
            return new WWW(url).texture;
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall, Obsolete("All blocking WWW functions have been deprecated, please use one of the asynchronous functions instead.", true)]
        public static extern string GetURL(string url);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void InitWWW(string url, byte[] postData, string[] iHeaders);
        public static WWW LoadFromCacheOrDownload(string url, int version)
        {
            uint crc = 0;
            return LoadFromCacheOrDownload(url, version, crc);
        }

        public static WWW LoadFromCacheOrDownload(string url, int version, uint crc)
        {
            return new WWW(url, version, crc);
        }

        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void LoadImageIntoTexture(Texture2D tex);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public extern void LoadUnityWeb();
        internal static Dictionary<string, string> ParseHTTPHeaderString(string input)
        {
            if (input == null)
            {
                throw new ArgumentException("input was null to ParseHTTPHeaderString");
            }
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            StringReader reader = new StringReader(input);
            int num = 0;
            while (true)
            {
                string str = reader.ReadLine();
                if (str == null)
                {
                    return dictionary;
                }
                if ((num++ == 0) && str.StartsWith("HTTP"))
                {
                    dictionary["STATUS"] = str;
                }
                else
                {
                    int index = str.IndexOf(": ");
                    if (index != -1)
                    {
                        string str2 = str.Substring(0, index).ToUpper();
                        string str3 = str.Substring(index + 2);
                        dictionary[str2] = str3;
                    }
                }
            }
        }

        public static string UnEscapeURL(string s)
        {
            Encoding e = Encoding.UTF8;
            return UnEscapeURL(s, e);
        }

        public static string UnEscapeURL(string s, Encoding e)
        {
            if (s == null)
            {
                return null;
            }
            if ((s.IndexOf('%') == -1) && (s.IndexOf('+') == -1))
            {
                return s;
            }
            return WWWTranscoder.URLDecode(s, e);
        }

        public AssetBundle assetBundle { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        public AudioClip audioClip
        {
            get
            {
                return this.GetAudioClip(true);
            }
        }

        public byte[] bytes { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        [Obsolete("Please use WWW.text instead")]
        public string data
        {
            get
            {
                return this.text;
            }
        }

        public string error { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        public bool isDone { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        public MovieTexture movie { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        [Obsolete(".oggVorbis accessor is deprecated, use .audioClip or GetAudioClip() instead.")]
        public AudioClip oggVorbis { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        public float progress { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        public Dictionary<string, string> responseHeaders
        {
            get
            {
                if (!this.isDone)
                {
                    throw new UnityException("WWW is not finished downloading yet");
                }
                return ParseHTTPHeaderString(this.responseHeadersString);
            }
        }

        private string responseHeadersString { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        public int size { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        public string text
        {
            get
            {
                if (!this.isDone)
                {
                    throw new UnityException("WWW is not ready downloading yet");
                }
                return this.GetTextEncoder().GetString(this.bytes);
            }
        }

        public Texture2D texture
        {
            get
            {
                return this.GetTexture(false);
            }
        }

        public Texture2D textureNonReadable
        {
            get
            {
                return this.GetTexture(true);
            }
        }

        public ThreadPriority threadPriority { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] set; }

        public float uploadProgress { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        public string url { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }
    }
}

